package restPROC;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;



import javax.ws.rs.core.Context;

import utilClasses.ListaPermisija;
import dao.daoPermisije;

@Path("/permisije") 
public class Permisije {
	@GET
	@Path("/odredjena")
	@Produces({"application/xml"})
	public ListaPermisija getUsers(@QueryParam("rola") String rola,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "getPerms");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ListaPermisija rez = new ListaPermisija();
		try {
			
			rez=daoPermisije.dajPermisije(rola);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	@GET
	@Path("/kreiraj")
	@Produces("text/plain")
	public String kreiraj(@QueryParam("permisija") String naziv,@QueryParam("rola") String rola,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "insertPerms");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String rez = null;
		try {
			
			rez=daoPermisije.insertPermisija(naziv,rola);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	/*@GET
	@Path("/moje")
	@Produces({"application/xml"})
	public ListaPermisija getMojePermisije(@QueryParam("rola") String rola)
	{
		ListaPermisija lper=null;
		try {
			
			lper=daoPermisije.dajSamoPermisijeKojeImam(rola);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lper;
	}*/
	/*@GET
	@Path("/broj")
	@Produces("text/plain")
	public String getBrojPermisija(@QueryParam("rola") String rola){
		String rez=new String(Integer.toString(0));
		try {
			
			rez=Integer.toString(daoPermisije.brojPermisija(rola));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
	}*/
	@GET
	@Path("/povezi")
	@Produces("text/plain")
	public String kreiraj(@QueryParam("permisija") String naziv,@QueryParam("rola") String rola,@QueryParam("brisem") String brisem,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "insertPerms");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String rez = null;
		try {
			
			rez=daoPermisije.updatePermisija(naziv,rola,brisem);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}

}
